#ifndef _VECTOR_H
#define _VECTOR_H
#include <iostream>
#include <algorithm>

template <typename Object> class Vector
{
//设定SPARE_CAPACITY
enum {SPARE_CAPACITY = 16};
//私有成员
private:
    int theSize;
    int theCapacity;
    Object* objects;
//公共成员
public:
    //参数构造
    explicit Vector(int initSize = 0)  : theSize{ initSize },theCapacity{ initSize + SPARE_CAPACITY }
    {objects = new Object[theCapacity];}

    Vector( const Vector& rhs ) : objects{nullptr}
    {
        operator=(rhs);
    }
    //析构函数
    ~Vector()
    {delete [] objects;}
    //重载"="
    const Vector& operator= (const Vector & rhs) 
    {
        if(this != &rhs)
        {
            delete [] objects;
            theSize = rhs.size();
            theCapacity = rhs.theCapacity;

            objects = new Object[capacity()];
            for(int i=0; i < size(); i++)
            {
                objects[i] = rhs.objects[i];
            }
        }

        return *this;
    }

    //改变size
    void resize(int newSize)
    {
        if(newSize > theCapacity)
        reserve(newSize * 2 + 1);
        theSize = newSize;
    }

    //改变capacity
    void reserve(int newCapacity)
    {
        if(newCapacity < theSize)
        return;
        Object* oldArray = objects;
        objects = new Object[newCapacity];
        for(int i = 0; i < theSize; i++)
            objects[i] = oldArray[i];
        
        theCapacity = newCapacity;
        delete [] oldArray;
    }
    //重载[](const和正常)
    Object& operator[](int index)
    {
        return objects[index];
    }
    const Object& operator[](int index) const
    {
        return objects[index];
    }
    //判断是否为空
    bool empty() const
    {
        return size() == 0;
    }
    //size大小
    int size() const
    {
        return theSize;
    }
    //capacity大小
    int capacity() const
    {
        return theCapacity;
    }
    //push_back函数（在最后面增加一个数）
    void push_back(const Object& x)
    {
        if(theSize == theCapacity)
        reserve(2 * theCapacity + 1);
        objects[theSize++] = x;
    }
    //pop_back函数（删除最后一个数）
    void pop_back()
    {
        theSize--;
    }
    //最后一个数字
    const Object& back()const
    {
        return objects[theSize-1];
    }

    ///写const和正常两个版本的指针
    typedef Object* iterator;
    typedef const Object* const_iterator;

    ///写const和正常两个版本
    iterator begin() {return &objects[0];}
    const_iterator begin() const {return &objects[0];}
    iterator end() {return &objects[size()];}
    const_iterator end() const {return &objects[size()];}
    
};

#else
#endif